package com.website.config.poi;

import com.website.system.model.Drug;
import com.website.system.model.Pharmacy;
import org.apache.poi.hpsf.DocumentSummaryInformation;
import org.apache.poi.hpsf.SummaryInformation;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class PoiUtils {
    /**
     * 导出
     * @param drugs
     * @return
     */
    public static ResponseEntity<byte[]> exportDrugs2Excel(List<Drug> drugs) {
        HttpHeaders headers = null;
        ByteArrayOutputStream baos = null;
        try {
            //1.创建Excel文档
            HSSFWorkbook workbook = new HSSFWorkbook();
            //2.创建文档摘要
            workbook.createInformationProperties();
            //3.获取文档信息，并配置
            DocumentSummaryInformation dsi = workbook.getDocumentSummaryInformation();
            //3.1文档类别
            dsi.setCategory("药品信息");
            //3.2设置文档管理员
            dsi.setManager("科比");
            //3.3设置组织机构
            dsi.setCompany("XXX药品集团");
            //4.获取摘要信息并配置
            SummaryInformation si = workbook.getSummaryInformation();
            //4.1设置文档主题
            si.setSubject("药品信息表");
            //4.2.设置文档标题
            si.setTitle("药品信息");
            //4.3 设置文档作者
            si.setAuthor("XXX集团");
            //4.4设置文档备注
            si.setComments("备注信息暂无");
            //创建Excel表单
            HSSFSheet sheet = workbook.createSheet("XXX集团药品信息表");
            //创建日期显示格式
            HSSFCellStyle dateCellStyle = workbook.createCellStyle();
            dateCellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));
            //创建标题的显示样式
            HSSFCellStyle headerStyle = workbook.createCellStyle();
            headerStyle.setFillForegroundColor(IndexedColors.YELLOW.index);
            headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
            //定义列的宽度
            sheet.setColumnWidth(0, 5 * 256);
            sheet.setColumnWidth(1, 15 * 256);
            sheet.setColumnWidth(2, 15 * 256);
            sheet.setColumnWidth(3, 15 * 256);
            sheet.setColumnWidth(4, 15 * 256);
            sheet.setColumnWidth(5, 15 * 256);
            sheet.setColumnWidth(6, 15 * 256);
            sheet.setColumnWidth(7, 15 * 256);
            //5.设置表头
            HSSFRow headerRow = sheet.createRow(0);
            HSSFCell cell0 = headerRow.createCell(0);
            cell0.setCellValue("药品编号");
            cell0.setCellStyle(headerStyle);
            HSSFCell cell1 = headerRow.createCell(1);
            cell1.setCellValue("药品名字");
            cell1.setCellStyle(headerStyle);
            HSSFCell cell2 = headerRow.createCell(2);
            cell2.setCellValue("图片");
            cell2.setCellStyle(headerStyle);
            HSSFCell cell3 = headerRow.createCell(3);
            cell3.setCellValue("药品效果");
            cell3.setCellStyle(headerStyle);
            HSSFCell cell4 = headerRow.createCell(4);
            cell4.setCellValue("上架时间");
            cell4.setCellStyle(headerStyle);
            HSSFCell cell5 = headerRow.createCell(5);
            cell5.setCellValue("修改时间");
            cell5.setCellStyle(headerStyle);
            HSSFCell cell6 = headerRow.createCell(6);
            cell6.setCellValue("价格");
            cell6.setCellStyle(headerStyle);
            HSSFCell cell7 = headerRow.createCell(7);
            cell7.setCellValue("所属类型");
            cell7.setCellStyle(headerStyle);
            //6.装数据
            for (int i = 0; i < drugs.size(); i++) {
                HSSFRow row = sheet.createRow(i + 1);
                Drug drug = drugs.get(i);
                row.createCell(0).setCellValue(drug.getId());
                row.createCell(1).setCellValue(drug.getName());
                row.createCell(2).setCellValue(drug.getUrl());
                row.createCell(3).setCellValue(drug.getEffect());
                row.createCell(4).setCellValue(drug.getBuytime());
                row.createCell(5).setCellValue(drug.getUpdatetime());
                row.createCell(6).setCellValue(drug.getPrice());
                row.createCell(7).setCellValue(drug.getPid());
            }
            headers = new HttpHeaders();
            headers.setContentDispositionFormData("attachment",
                    new String("药品表.xls".getBytes("UTF-8"), "iso-8859-1"));
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            baos = new ByteArrayOutputStream();
            workbook.write(baos);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new ResponseEntity<byte[]>(baos.toByteArray(), headers, HttpStatus.CREATED);
    }
    public static List<Drug> importDrug2List(MultipartFile file) {
        List<Drug> drugs = new ArrayList<>();
        try {
            HSSFWorkbook workbook = new HSSFWorkbook(new POIFSFileSystem(file.getInputStream()));
            int numberOfSheets = workbook.getNumberOfSheets();
            for (int i = 0; i < numberOfSheets; i++) {
                HSSFSheet sheet = workbook.getSheetAt(i);
                int physicalNumberOfRows = sheet.getPhysicalNumberOfRows();
                Drug f;
                for (int j = 0; j < physicalNumberOfRows; j++) {
                    if (j == 0) {
                        continue;//标题行
                    }
                    HSSFRow row = sheet.getRow(j);
                    if (row == null) {
                        continue;//没数据
                    }
                    int physicalNumberOfCells = row.getPhysicalNumberOfCells();
                    f = new Drug();
                    for (int k = 0; k < physicalNumberOfCells; k++) {
                        HSSFCell cell = row.getCell(k);
                        switch (cell.getCellTypeEnum()) {
                            case STRING: {
                                String cellValue = cell.getStringCellValue();
                                if (cellValue == null) {
                                    cellValue = "";
                                }
                                switch (k) {
                                    case 1:
                                        f.setName(cellValue);
                                        break;
                                    case 2:
                                        f.setUrl(cellValue);
                                        break;
                                    case 3:
                                        f.setEffect(cellValue);
                                        break;
                                    case 4:
                                        f.setBuytime(cellValue);
                                        break;
                                    case 5:
                                        f.setUpdatetime(cellValue);
                                        break;
                                }
                            }
                            break;
                            default:{
                                switch (k){
                                    case 6:
                                        f.setPrice(cell.getNumericCellValue());
                                        break;
                                    case 7:
                                        f.setPid((int)cell.getNumericCellValue());
                                        break;
                                }
                            }
                        }
                    }
                    drugs.add(f);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return drugs;
    }
}